package com.zhengdongzulin.zuke.app.main.apartment.sign

import android.content.Intent
import android.view.Gravity
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.lxj.androidktx.core.*
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ReserveSearchListData
import com.zhengdongzulin.zuke.app.main.apartment.ZKReserveYuDingActivity
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.databinding.ZkActivityReserveSearchBinding
import com.zhengdongzulin.zuke.ext.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * 房型房间列表搜索
 */
class ZKReserveSearchActivity : BaseActivity<ReserveSearchVm, ZkActivityReserveSearchBinding>() {
    private lateinit var mDirectionPop: BasePopupWindow
    var mData = mutableListOf<ReserveSearchListData>()
    var yuDing = false
    var phone = ""

    override fun getLayoutResId(): Int = R.layout.zk_activity_reserve_search

    override fun initVM(): ReserveSearchVm = getVM(ReserveSearchVm::class.java)

    override fun initView() {

        yuDing = intent.getBooleanExtra("yuDing", false)

        var houseItemName = intent.getStringExtra("houseItemName")
        var roomTypeName = intent.getStringExtra("roomTypeName")
        phone = intent.getStringExtra("phone").toString()

        mView.tvMendian.text = houseItemName
        mView.tvFangxing.text = roomTypeName

        //mViewModel.room_type_house_list(intent.getStringExtra("houseId")!!)
        mView.ivTitle.loadImage(intent.getStringExtra("url"), R.mipmap.error_empty_zheng)
//        mView.recyclerView.vertical()
//            .bindData(mData, R.layout.item_reserve_search) { holder, t, position ->
//                holder.setText(R.id.name, "${t.houseName}")
//                holder.setText(R.id.desc, "${t.doorName}\u0020${t.area}㎡\u0020${t.orientation}")
//                var money = holder.getView<TextView>(R.id.money)
//                money.text = "￥${t.rentPrice}"
//                money.appendSizeSpan("/月", 0.46f)
//                    .colorSpan(
//                        "",
//                        money.text.length - 2..money.text.length,
//                        color(R.color.black_66)
//                    )
//                    .styleSpan("", 0..money.text.length - 2)
//            }.itemClick { data: List<ReserveSearchListData>, holder, position ->
//                if (yuDing) {
//                    //跳转到预订界面
//                    var tem = mShareViewModel.houseDetails.value
//                    mShareViewModel.houseDetails.value = tem?.apply {
//                        id = data[position].id
//                    }
//                    Intent().put("type", "houseDetails").start(
//                        this,
//                        ZKReserveYuDingActivity::class.java
//                    )
//                } else {
//                    Intent().put("houseId", data[position].id)
//                        .start(this, ZKSign1Activity::class.java)
//                }
//            }
    }

    override fun initData() {
        mViewModel.room_type_house_list.observe(this, Observer {
            mData.clear()
            if (it != null && it.size > 0) {
                mData.addAll(it)
                //mView.recyclerView.adapter?.notifyDataSetChanged()
                showPopwindow(mData)
            } else {
                toast("未查询到相关房源")
            }
        })
        mViewModel.room_type_house_list.state.observe(
            this,
            getObserver(mViewModel.room_type_house_list)
        )
    }

    override fun initListener() {
//        mView.clear.setOnClickListener {
//            mView.search.setText("")
//        }
//        mView.tvSearch.setOnClickListener {
//            val fangNoLike = mView.search.getTextZ()
//            if (fangNoLike.isNullOrEmpty()) {
//                return@setOnClickListener
//            }
//            mViewModel.room_type_house_list(intent.getStringExtra("houseId")!!, fangNoLike)
//        }
//        mView.search.addTextChangedListener(object : TextWatcher {
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//            }
//
//            override fun afterTextChanged(s: Editable?) {
//                if (mView.search.isEmpty()) {
//                    mData.clear()
//                    mView.recyclerView.adapter?.notifyDataSetChanged()
//                }
//            }
//        })

        mView.llPhone.setOnClickListener {
            callPhoneDialog("电话咨询", phone).show()
        }
        mView.etFangjianhao.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val fangNoLike = mView.etFangjianhao.getTextZ()
                if (!fangNoLike.isNullOrEmpty()) {
                    mViewModel.room_type_house_list(intent.getStringExtra("houseId")!!, fangNoLike)
                }
            }
            return@setOnEditorActionListener false
        }
    }

    override fun startObserve() {
    }

    fun showPopwindow(array: MutableList<ReserveSearchListData>) {
        val viewGroup = layoutInflater.inflate(R.layout.popwindow_roometype_choose_house, null)
        var rv1 = viewGroup.findViewById<RecyclerView>(R.id.rv1)
        rv1.vertical(1).bindData(
            array,
            R.layout.item_popwidow_roomtype_search_room
        ) { holder, item, position ->
            val view = holder.getView<TextView>(R.id.name)
            view.text = item.houseName
        }.itemClick { data: List<ReserveSearchListData>, holder, position ->
            if (yuDing) {
                //跳转到预订界面
                var tem = mShareViewModel.houseDetails.value
                mShareViewModel.houseDetails.value = tem?.apply {
                    id = data[position].id
                }
                Intent().put("type", "houseDetails").start(
                    this,
                    ZKReserveYuDingActivity::class.java
                )
            } else {
                //屏蔽在线签约
//                Intent().put("houseId", data[position].id)
//                    .start(this, ZKSign1Activity::class.java)
            }
            mDirectionPop?.dismiss()
        }
        mDirectionPop = object : BasePopupWindow(this) {
            override fun onCreateShowAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.FROM_TOP)
                    .toShow()
            }

            override fun onCreateDismissAnimation(): Animation {
                return AnimationHelper.asAnimation()
                    .withTranslation(TranslationConfig.TO_TOP)
                    .toDismiss()
            }
        }.apply {
            contentView = viewGroup
            setPopupGravity(Gravity.BOTTOM)
            setAlignBackground(true)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
        }
        mDirectionPop.showPopupWindow(mView.etFangjianhao)
    }
}